/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2025-06-06     jerry       the first version
 */

#include <rtthread.h>
#include <rtdebug.h>
#include <rtdbg.h>
#include <webclient.h>

#define URL_LEN_MAX (0x100*sizeof(unsigned char))
#define RESP_BUF_SIZE (URL_LEN_MAX)
#define HEADER_BUF_SIZE (URL_LEN_MAX)

#define MAX(a,b) (((a)>(b))?(a):(b))
#define MIN(a,b) (((a)>(b))?(b):(a))

rt_err_t post(void*content, size_t len, int end)
{
    struct webclient_session*session = RT_NULL;
    char* photo_post_url = RT_NULL;
    unsigned char *resp_buffer = RT_NULL;
    int index, ret = 0;
    int bytes_read, resp_status;
    int content_length = -1;

    photo_post_url = rt_calloc(1, URL_LEN_MAX);
    rt_snprintf(photo_post_url, URL_LEN_MAX,
            "http://101.37.75.201:8000/api/post?end=%d",
            end);

    buffer = (unsigned char*) web_malloc(RESP_BUF_SIZE);
    if (buffer == RT_NULL)
    {
        LOG_E("no mem for recv buffer.\n");
        ret = -RT_ENOMEM;
        goto _exit;
    }
    session = webclient_session_create(HEADER_BUF_SIZE);
    if (session == RT_NULL)
    {
        LOG_E("session creating failed.\n");
        ret = -RT_ENOMEM;
        goto _exit;
    }

    // 设置请求头
    char content_type[] =
            "Content-Type: application/x-www-form-urlencoded\r\n";
    int content_type_result = webclient_header_fields_add(
            session, content_type);
    if (content_type_result < 0)
    {
        LOG_E(
                "Add Content-Type header failed, error code: %d",
                content_type_result);
        goto _exit;
    }

    char content_length[64];
    rt_snprintf(content_length, sizeof(content_length),
            "Content-Length: %ld\r\n", len);
    int content_length_result = webclient_header_fields_add(
            session, content_length);
    if (content_length_result < 0)
    {
        LOG_E(
                "Add Content-Length header failed, error code: %d",
                content_length_result);
        goto _exit;
    }
    //try to connect to server
    for (int attempt = 0; attempt < 5; attempt++)
    {
        LOG_I("Send GET request to %s"
                , photo_post_url);
        resp_status = webclient_get(session,
                photo_post_url);
        if (resp_status != 200)
        {
            LOG_E(
                    "webclient GET request failed, response(%d) error.\n",
                    resp_status);
            ret = -RT_ERROR;
            continue;

        }
        else
        {
            ret = RT_EOK;
            break;
        }
    }
    if (ret != RT_EOK) goto _exit;

    content_length = webclient_content_length_get(session);

    content _length = webclient_content_length_get(session);
    if (content_length < 0)
    {
        rt_kprintf(
                "webclient GET request type is chunked. \n");
        do
        {
            bytes_read = webclient_read(session,
                    (void *) buffer, GET_RESP_BUFSZ);
            if (bytes_read <= 0)
            {
                break;
            }
            for (index = 0; index < bytes_read; index++)

            {
                rt_kprintf("%c", buffer[index]);
            }

        } while (1);
        rt_kprintf("\n");
    }
    else
    {
        int content_pos = 0;
        do
        {
            int read_length = MIN(
                    content_length - content_pos,
                    RESP_BUF_SIZE);
            bytes_read = webclient_read(session,
                    (void *) buffer, read_length);
            if (bytes_read <= 0)
            {
                break;
            }
            for (index = 0; index < bytes_read; index++)
            {
                rt_kprintf("%c", buffer[index]);
            }
        } while (content_pos < content_length);
    }
    _exit: rt_free(photo_post_url);
    if (session)
    {
        webclient_close(session);
        session = RT_NULL;
    }
    if (buffer)
    {
        web_free(buffer);
        buffer = RT_NULL;
    }
    return ret;
}
